package com.adb.indian.rto.uc;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

import com.adb.indian.rto.R;
import com.adb.indian.rto.utility.Log;

public class ITextView extends TextView{
	
	private final String TAG = ITextView.class.getCanonicalName();
	
	public ITextView(Context context) {
		super(context);
	}
	
	public ITextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		setCustomFont(context,attrs);
	}

	public ITextView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		setCustomFont(context, attrs);
	}
	
	private void setCustomFont(Context mContext,AttributeSet mAttrs){
		
		if(mAttrs == null)
		return;
		
		//Get Styled for Particular custom view  in XML 
		TypedArray mTypedArray = mContext.obtainStyledAttributes(mAttrs,R.styleable.ITextView);
		// Get value of give style Attributes of custom view in XML 
		String appFontsName = mTypedArray.getString(R.styleable.ITextView_customFont);
		
		if(appFontsName.length() > 0 || appFontsName != null){
			setCustomFontsToView(mContext, appFontsName);
			mTypedArray.recycle();
		}
		
		
	}
	
	public boolean setCustomFontsToView(Context mContext,String asstesFontName){
		Typeface mTypeface = null;
		
		//This is create new custom font [ true type fonts] access from assets folder
		StringBuilder mPathBuilder = new StringBuilder("fonts/");
		mPathBuilder.append(asstesFontName);
		Log.debug(TAG,"[ Fonts Asstes Path ] " + mPathBuilder.toString() + "[ selected Fonts Name ] "+ asstesFontName);
		
		try {
			mTypeface = Typeface.createFromAsset(mContext.getAssets(),mPathBuilder.toString());
			
		} catch (Exception e) {
			Log.debug(TAG,"[ Unable to Load Fonts ]");
			e.printStackTrace();
			return false;
		}
		
		//If every thing is ok then set typeface to view 
		setTypeface(mTypeface);
		return true;
	}

}
